﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using Noris.Schedule.Support;
using Noris.Tools.FrxEditor.Editor;
using Noris.Tools.FrxEditor.Components;
using Noris.Tools.FrxEditor.Forms;
using Noris.Tools.FrxEditor.ComponentsLayer;

namespace Noris.Tools.FrxEditor.Data
{
    /// <summary>
    /// Datová obálka editovaného dokumentu. 
    /// Ta se ukládá do XML dat, tu lze renderovat do sestavy.
    /// Slouží jako zdroj/cíl dat pro editaci.
    /// </summary>
    public partial class ReportDocument : IRevertContainer
    {
        #region Document New, Save, Save
        /// <summary>
        /// Řešení akcí z UI typu Document
        /// </summary>
        /// <param name="args"></param>
        private void CmdDocument(EditorCommandExecuteArgs args)
        {
            EditorCommandDocumentType action;
            if (!args.CommandKey.TryGetSpecification<EditorCommandDocumentType>(out action))
                return;

            switch (action)
            {
                case EditorCommandDocumentType.DocumentNew:
                    this.CmdDocumentNew();
                    break;
                case EditorCommandDocumentType.DocumentOpen:
                    this.CmdDocumentOpen();
                    break;
                case EditorCommandDocumentType.DocumentSave:
                    this.CmdDocumentSave(this.DocumentFile, false);
                    break;
                case EditorCommandDocumentType.DocumentSaveAs:
                    this.CmdDocumentSave(this.DocumentFile, true);
                    break;
                case EditorCommandDocumentType.DocumentPreview:
                    this.CmdDocumentPreview();
                    break;
                case EditorCommandDocumentType.DocumentEdit:
                    this.CmdDocumentEditor();
                    break;
                case EditorCommandDocumentType.DocumentEditRootData:
                    this.CmdDocumentEditRootData();
                    break;
            }
        }
        private void CmdDocumentNew()
        {
            this.ClearAll(DocumentNewPresetMode.StandardReportBand);
            this.RootBand.ReportData.RootClassNumber = 1180;
            this.UIRefresh();
            this.ReDraw();
        }
        /// <summary>
        /// Otevře dokument dle výběru uživatele
        /// </summary>
        private void CmdDocumentOpen()
        {
            string sourceFile = this._DocumentFindSourceFile();
            if (!String.IsNullOrEmpty(sourceFile))
                this._DocumentLoadFromFile(sourceFile);
        }
        /// <summary>
        /// Zobrazí ReportData pro RootBand
        /// </summary>
        private void CmdDocumentEditRootData()
        {
            if (this.RootBand != null && this.RootBand.ReportData != null)
                this.RootBand.ReportData.ShowSettings();
        }
        /// <summary>
        /// Soubor, z něhož byl soubor načten / do něhož se ukládal naposledy.
        /// Při importu z formátu SRD je dokument konvertován a zde je připraveno jméno s příponou ".frx.xml"
        /// </summary>
        public string DocumentFile { get; set; }
        /// <summary>
        /// Metoda přidá svůj díl informací pro Refresh UI, a to za prvky UI které jsou ovlivněné stavem Clipboardu
        /// </summary>
        /// <param name="itemStateList">Pole informací o položkách UI a jejich aktuálním stavu</param>
        /// <param name="selectedCount">Počet vybraných prvků</param>
        private void UIRefreshDocument(List<EditorUIItemState> itemStateList, int selectedCount)
        {
            bool enableEditor = (this.Form.EditorContent == EditorForm.EditorContentType.Editor);
            bool enablePreview = (this.Form.EditorContent == EditorForm.EditorContentType.PdfPreview);

            itemStateList.Add(new EditorUIItemState(EditorCommandType.Document, EditorCommandDocumentType.DocumentNew) { Enabled = enableEditor });
            itemStateList.Add(new EditorUIItemState(EditorCommandType.Document, EditorCommandDocumentType.DocumentOpen) { Enabled = enableEditor });
            itemStateList.Add(new EditorUIItemState(EditorCommandType.Document, EditorCommandDocumentType.DocumentSave) { Enabled = enableEditor });
            itemStateList.Add(new EditorUIItemState(EditorCommandType.Document, EditorCommandDocumentType.DocumentSaveAs) { Enabled = enableEditor });
            itemStateList.Add(new EditorUIItemState(EditorCommandType.Document, EditorCommandDocumentType.DocumentRevert) { Enabled = enableEditor });
            itemStateList.Add(new EditorUIItemState(EditorCommandType.Document, EditorCommandDocumentType.DocumentClose) { Enabled = enableEditor });
            itemStateList.Add(new EditorUIItemState(EditorCommandType.Document, EditorCommandDocumentType.DocumentPreview) { Enabled = enableEditor });
            itemStateList.Add(new EditorUIItemState(EditorCommandType.Document, EditorCommandDocumentType.DocumentPrint) { Enabled = enableEditor || enablePreview });
            itemStateList.Add(new EditorUIItemState(EditorCommandType.Document, EditorCommandDocumentType.DocumentEdit) { Enabled = enablePreview });
        }
        #endregion
        #region Hledání souboru
        /// <summary>
        /// Pokusí se najít soubor daného jména v adresáři reportů
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string FindReportFile(string name)
        {
            string path = FindReportsDir();
            if (path == null) return null;

            string searchPattern = System.IO.Path.GetFileName(name);
            string[] files = System.IO.Directory.GetFiles(path, searchPattern, System.IO.SearchOption.AllDirectories);
            if (files == null || files.Length == 0) return null;

            return files[0];
        }
        /// <summary>
        /// Vrátí adresář, kde jsou uloženy soubory Reportů
        /// </summary>
        /// <returns></returns>
        public static string FindReportsDir()
        {
            return SystemDialogs.SearchNearestPath(System.IO.Path.GetDirectoryName(Application.ExecutablePath), "Reports");
        }
        #endregion
        #region Load file XML content, load XML string
        /// <summary>
        /// Vytvoří a vrátí dokument z uvedeného jména souboru
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static ReportDocument CreateFromFile(string fileName)
        {
            ReportDocument document = new ReportDocument();
            if (!fileName.Contains('\\'))
                fileName = FindReportFile(fileName);
            document._DocumentLoadFromFile(fileName);
            return document;
        }
        /// <summary>
        /// Vytvoří a vrátí dokument z předaného obsahu
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static ReportDocument CreateFromContent(string content)
        {
            ReportDocument document = new ReportDocument();
            document._DocumentLoadFromContent(content, null);
            return document;
        }
        /// <summary>
        /// Načte obsah 
        /// </summary>
        /// <param name="fileName"></param>
        private void _DocumentLoadFromFile(string fileName)
        {
            string content = System.IO.File.ReadAllText(fileName, Encoding.UTF8);
            this._DocumentLoadFromContent(content, fileName);
        }
        /// <summary>
        /// Načte report z dodaného obsahu. Provede detekci formátu.
        /// </summary>
        /// <param name="content"></param>
        /// <param name="fileName"></param>
        private void _DocumentLoadFromContent(string content, string fileName)
        {
            DocumentVersionType version = this._DocumentDetectVersion(content);
            switch (version)
            {
                case DocumentVersionType.PowerBuilder700:
                    this.ClearPropertyGrid();
                    this._DocumentLoadFromContentSrd(content, fileName);
                    return;
                case DocumentVersionType.FrxXml100:
                    this.ClearPropertyGrid();
                    this._DocumentLoadFromContentFrx100(content, fileName);
                    return;
                default:
                    throw new DataException("Zvolený soubor neobsahuje žádný známý formát.");
            }
        }
        /// <summary>
        /// Načte report z dodaného obsahu ve formátu PowerBuilder 7.00 SRD
        /// </summary>
        /// <param name="content"></param>
        /// <param name="fileName"></param>
        private void _DocumentLoadFromContentSrd(string content, string fileName)
        {
            if (fileName != null && System.IO.File.Exists(fileName))
                content = System.IO.File.ReadAllText(fileName, Encoding.Default);   // Původně byl content načten s Encoding.UTF8, ale to pro SRD není správně.
            List<Noris.Schedule.Support.Parser.ParserSegment> srdSegments = Noris.Tools.FrxEditor.Data.Pb7Loader.GetReportSrdDataFromContent(content);
            Noris.Tools.FrxEditor.Data.Pb7Loader.ConvertSrdToDocument(srdSegments, this, fileName);
            this.ReDraw();
        }
        /// <summary>
        /// Načte report z dodaného obsahu ve formátu FRX.XML 1.00
        /// </summary>
        /// <param name="content"></param>
        /// <param name="fileName"></param>
        private void _DocumentLoadFromContentFrx100(string content, string fileName)
        {
            ConvertFrxToDocument(content, this, fileName);
            this.ReDraw();
        }
        /// <summary>
        /// Z dodaného XML naplní data do daného dokumentu.
        /// </summary>
        /// <param name="content"></param>
        /// <param name="document"></param>
        /// <param name="fileName"></param>
        public static void ConvertFrxToDocument(string content, ReportDocument document, string fileName)
        {
            string currentContent = document.DocumentGetXmlContent();
            try
            {
                document.ClearAll();
                document.DocumentSetXmlContent(content);
                document.DocumentFile = fileName;
            }
            catch (Exception)
            {
                document.DocumentSetXmlContent(currentContent);
                throw;
            }
        }
        /// <summary>
        /// Detekuje typ obsahu podle prvních dvou řádků
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        private DocumentVersionType _DocumentDetectVersion(string content)
        {
            if (String.IsNullOrEmpty(content)) return DocumentVersionType.None;
            string[] lines = content.Substring(0, 512).Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            if (lines.Length < 2) return DocumentVersionType.None;

            if (lines[0].Trim().StartsWith(Pb7Loader.SRD_HEADER) &&
                lines.Any(line => line.StartsWith(Pb7Loader.SRD_RELEASE)))
                return DocumentVersionType.PowerBuilder700;

            if (lines[0] == "<report Version=\"1.00\" Content=\"Document\">" &&
                lines[1].Trim().StartsWith("<properties "))
                return DocumentVersionType.FrxXml100;

            return DocumentVersionType.None;
        }
        /// <summary>
        /// Najde vstupní existující soubor pro načtení
        /// </summary>
        /// <returns></returns>
        private string _DocumentFindSourceFile()
        {
            string path = _LastPath;
            if (String.IsNullOrEmpty(path))
                path = FindReportsDir();
            string filter = "Report *.XML|*.xml|PowerBuilder *.SRD|*.SRD|All files *.*|*.*";
            int filterIndex = _LastFilt;
            string file = null;
            using (OpenFileDialog ofd = SystemDialogs.GetOpenFileDialog("Open report file...", filter, filterIndex, path, false))
            {
                DialogResult result = ofd.ShowDialog();
                if (result == DialogResult.OK)
                    file = ofd.FileName;
                filterIndex = ofd.FilterIndex;
            }
            if (file == null) return null;

            _LastPath = System.IO.Path.GetDirectoryName(file);
            _LastFilt = filterIndex;

            return file;
        }
        private static string _LastPath = null;
        private static int _LastFilt = 0;
        #endregion
        #region Save data to XML string, to file
        /// <summary>
        /// Uloží obsah do souboru
        /// </summary>
        /// <param name="targetFile"></param>
        /// <param name="forceDialog">Povinný dialog SaveAs</param>
        private void CmdDocumentSave(string targetFile, bool forceDialog)
        {
            if (forceDialog || String.IsNullOrEmpty(targetFile))
                targetFile = this._DocumentFindTargetFile(targetFile);
            if (!String.IsNullOrEmpty(targetFile))
            {
                string content = this.DocumentGetXmlContent();
                System.IO.File.WriteAllText(targetFile, content, Encoding.UTF8);
                this.DocumentFile = targetFile;
            }
        }
        /// <summary>
        /// Najde výstupní soubor pro uložení.
        /// Pokud si uživatel vyberene, vrací se null.
        /// Vrácený soubor má příponu ".frx.xml".
        /// </summary>
        /// <returns></returns>
        private string _DocumentFindTargetFile(string targetFile)
        {
            string initialDir = null;
            string initialFile = null;
            if (String.IsNullOrEmpty(targetFile))
            {
                initialDir = FindReportsDir();
                initialFile = null;
            }
            else
	        {
                initialDir = System.IO.Path.GetDirectoryName(targetFile);
                initialFile = System.IO.Path.GetFileName(targetFile);
            }
            using (SaveFileDialog sfd = SystemDialogs.GetSaveFileDialog("Save report file...", "Report *.XML|*.xml|All files *.*|*.*", initialDir, initialFile, ".xml"))
            {
                DialogResult result = sfd.ShowDialog();
                if (result == DialogResult.OK)
                {
                    string fileName = sfd.FileName;
                    if (fileName.EndsWith(XML_FILE_EXTENSION, StringComparison.InvariantCultureIgnoreCase))
                        return fileName;
                    return fileName + XML_FILE_EXTENSION;
                }
            }
            return null;
        }
        /// <summary>
        /// Z dokumentu this vytvoří jeho obsah ve formátu XML, a vrátí jej.
        /// </summary>
        public string DocumentGetXmlContent()
        {
            // XML dokument:
            XElement xReport = new XElement(XML_REPORT);
            xReport.Add(new XAttribute(XML_VERSION, XML_VERSION_V100));
            xReport.Add(new XAttribute(XML_CONTENT, XML_CONTENT_DOC));
            XDocument xdoc = new XDocument(xReport);

            // Vlastnosti reportu:
            XElement xProperties = new XElement(XML_PROPERTIES);
            this.Properties.XmlSerializeTo(xProperties);
            xdoc.Root.Add(xProperties);

            // Grid:
            XElement xReportGrid = new XElement(XML_GRID);
            this.ReportGrid.XmlSerializeTo(xReportGrid);
            xdoc.Root.Add(xReportGrid);

            // Bands:
            XElement xBands = new XElement(XML_BANDS);
            foreach (ReportBand band in this.BandDict.Values)
            {
                XElement xBand = new XElement(XML_BAND);
                band.XmlSerializeTo(xBand);
                xBands.Add(xBand);
            }
            xdoc.Root.Add(xBands);

            // Komponenty:
            XElement xComponents = new XElement(XML_COMPONENTS);
            foreach (ReportComponent comp in this.ComponentDict.Values)
            {
                XElement xComponent = new XElement(XML_COMPONENT);
                comp.XmlSerializeTo(xComponent);
                xComponents.Add(xComponent);
            }
            xdoc.Root.Add(xComponents);

            return xdoc.ToString();
        }
        /// <summary>
        /// Do this dokumentu vloží obsah z dodaného stringu.
        /// String má být ve formátu XML, tak jak jej vytvořila metoda DocumentGetXmlContent.
        /// </summary>
        /// <param name="content"></param>
        public void DocumentSetXmlContent(string content)
        {
            this.ClearAll(DocumentNewPresetMode.None);
            XElement xDoc = XElement.Parse(content, LoadOptions.None);
            foreach (XElement xElement in xDoc.Elements())
            {
                string name = xElement.Name.LocalName;
                switch (name)
                {
                    case XML_PROPERTIES:
                        this.Properties.XmlFillFrom(xElement);
                        break;
                    case XML_GRID:
                        this.ReportGrid.XmlFillFrom(xElement, true);
                        break;
                    case XML_BANDS:
                        foreach (XElement xBand in xElement.Elements())
                        {
                            if (xBand.Name.LocalName == XML_BAND)
                                this._DocumentLoadXmlBand(xBand);
                        }
                        break;
                    case XML_COMPONENTS:
                        foreach (XElement xComponent in xElement.Elements())
                        {
                            if (xComponent.Name.LocalName == XML_COMPONENT)
                                this._DocumentLoadXmlComponent(xComponent);
                        }
                        break;
                }
            }
        }
        /// <summary>
        /// Načte z XML nový Band a vloží jej do dokumentu.
        /// </summary>
        /// <param name="xBand"></param>
        private void _DocumentLoadXmlBand(XElement xBand)
        {
            ReportBand band = ReportBand.XmlDeserializeFrom(xBand, true);
            if (band == null)
                throw new FrxException("Bad format of ReportBand, in XML element: " + xBand.Value);
            this.AddBand(band);
        }
        /// <summary>
        /// Načte z XML nový Component a vloží jej do dokumentu.
        /// </summary>
        /// <param name="xComponent"></param>
        private void _DocumentLoadXmlComponent(XElement xComponent)
        {
            ReportComponent component = ReportComponent.XmlDeserializeFrom(this, xComponent, true);
            if (component == null)
                throw new FrxException("Bad format of ReportComponent, in XML element: " + xComponent.Value);
            this.AddComponent(component);
        }
        /// <summary>
        /// Vrátí string hodnotu z daného atributu, pokud nenajde vrací prázdný string.
        /// </summary>
        /// <param name="node"></param>
        /// <param name="attributeName"></param>
        /// <returns></returns>
        internal static string XmlGetXAttributeValue(XElement node, string attributeName)
        {
            return XmlGetXAttributeValue(node, attributeName, "");
        }
        /// <summary>
        /// Vrátí string hodnotu z daného atributu, pokud nenajde vrací prázdný string.
        /// </summary>
        /// <param name="node"></param>
        /// <param name="attributeName"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        internal static string XmlGetXAttributeValue(XElement node, string attributeName, string defaultValue)
        {
            XAttribute attribute = node.Attribute(attributeName);
            if (attribute == null) return defaultValue;
            if (attribute.Value == null) return defaultValue;
            return attribute.Value;
        }
        /// <summary>
        /// Vrátí true, pokud daný node obsahuje atribut daného jména
        /// </summary>
        /// <param name="node"></param>
        /// <param name="attributeName"></param>
        /// <returns></returns>
        internal static bool XmlContainAttribute(XElement node, string attributeName)
        {
            XAttribute attribute = node.Attribute(attributeName);
            if (attribute == null) return false;
            if (attribute.Value == null) return false;
            return true;
        }
        #region XML Konstanty
        internal const string XML_FILE_EXTENSION = ".frx.xml";
        internal const string XML_VERSION = "Version";
        internal const string XML_VERSION_V100 = "1.00";
        internal const string XML_CONTENT = "Content";
        internal const string XML_CONTENT_DOC = "Document";
        internal const string XML_REPORT = "report";
        internal const string XML_PROPERTIES = "properties";
        internal const string XML_GRID = "grid";
        internal const string XML_BANDS = "bands";
        internal const string XML_BAND = "band";
        internal const string XML_COMPONENTS = "components";
        internal const string XML_COMPONENT = "component";
        #endregion
        #endregion
        #region enum DocumentVersionType
        /// <summary>
        /// Verze souboru
        /// </summary>
        protected enum DocumentVersionType
        {
            /// <summary>Neurčeno</summary>
            None,
            /// <summary>PowerBuilder 7.00</summary>
            PowerBuilder700,
            /// <summary>FrxXml 1.00</summary>
            FrxXml100
        }
        #endregion
        #region Sample documents generator
        /// <summary>
        /// Vytvoří a vrátí sample dokument, jednoduchý
        /// </summary>
        /// <returns></returns>
        public static ReportDocument CreateSampleDocument1()
        {
            ReportDocument doc = new ReportDocument();
            /*
            int headerId = doc.AddBand(new ReportBand() { LogicalHeight = 35f, HeaderBackColor = Color.MintCream, HeaderText = "PageHeader" });
            int bodyId = doc.AddBand(new ReportBand() { LogicalHeight = 22f, LogicalWidth = 105f, HeaderText = "Detail Row" });
            int footerId = doc.AddBand(new ReportBand() { LogicalHeight = 35f, HeaderBackColor = Color.SeaShell, HeaderText = "PageFooter" });

            doc.AddComponent(new ReportComponentOrthogon() { OwnerTopBandId = headerId, Bounds = new RectangleF(2f, 3f, 210f, 9f), EditorRenderStyle = RenderStyle.FlatStyle, EditorBackColor = Color.FromArgb(255, 240, 230, 250), EditorColorShift = -20 });
            doc.AddComponent(new ReportComponentOrthogon() { OwnerTopBandId = headerId, Bounds = new RectangleF(2f, 15, 210f, 9f), EditorRenderStyle = RenderStyle.FlatStyle, EditorBackColor = Color.FromArgb(255, 240, 230, 250), EditorColorShift = -20 });
            doc.AddComponent(new ReportComponentOrthogon() { OwnerTopBandId = headerId, Bounds = new RectangleF(15f, 20f, 50f, 8.2f), EditorBackColor = Color.NavajoWhite });

            doc.AddComponent(new ReportComponentOrthogon() { OwnerTopBandId = bodyId, Bounds = new RectangleF(24f, 26f, 40f, 10f) });
            doc.AddComponent(new ReportComponentOrthogon() { OwnerTopBandId = bodyId, Bounds = new RectangleF(18f, 32f, 35f, 9f) });
            doc.AddComponent(new ReportComponentOrthogon() { OwnerTopBandId = bodyId, Bounds = new RectangleF(208f, 295f, 4f, 4f), EditorBackColor = Color.Red });
            doc.AddComponent(new ReportComponentOrthogon() { OwnerTopBandId = bodyId, Bounds = new RectangleF(40f, 45f, 15f, 7f), EditorBackColor = Color.LightBlue, InteractivityMode = EditItemInteractivityMode.StandardResponse | EditItemInteractivityMode.MoveX | EditItemInteractivityMode.ResizeX });
            doc.AddComponent(new ReportComponentOrthogon() { OwnerTopBandId = bodyId, Bounds = new RectangleF(40f, 56f, 7f, 15f), EditorBackColor = Color.LightBlue, InteractivityMode = EditItemInteractivityMode.StandardResponse | EditItemInteractivityMode.MoveY | EditItemInteractivityMode.ResizeY });

            doc.AddComponent(new ReportComponentLabel() { OwnerTopBandId = footerId, Bounds = new RectangleF(7f, 2f, 29f, 6f), EditorBackColor = Color.FromArgb(0, 255, 255, 255), EditorColorShift = 0, InteractivityMode = EditItemInteractivityMode.StandardActiveResizeX, Text = "Popisek položky:" });
            doc.AddComponent(new ReportComponentLabel() { OwnerTopBandId = footerId, Bounds = new RectangleF(35f, 2f, 29f, 6f), EditorBackColor = Color.FromArgb(0, 255, 255, 255), EditorColorShift = 0, InteractivityMode = EditItemInteractivityMode.StandardActiveResizeX, IsLocked = true, Text = "Zamčená položka" });
            doc.AddComponent(new ReportComponentImage() { OwnerTopBandId = footerId, Bounds = new RectangleF(20f, 60f, 30f, 50f), EditorBackColor = Color.LightGray, EditorColorShift = 0, Image = Image.FromFile(@"C:\David\Images\bloc_notes.gif") });
            doc.AddComponent(new ReportComponentImage() { OwnerTopBandId = footerId, Bounds = new RectangleF(90f, 60f, 30f, 12f), EditorBackColor = Color.LightGray, EditorColorShift = 0, Image = Image.FromFile(@"C:\David\Images\Flag_Britan.GIF") });
            */
            return doc;
        }
        /// <summary>
        /// Vytvoří a vrátí sample dokument, střední
        /// </summary>
        /// <returns></returns>
        public static ReportDocument CreateSampleDocument2()
        {
            ReportDocument doc = new ReportDocument();

            doc.ClearAll();

            int headerId = doc.AddBand(new ReportBand(ReportBandType.PageHeader) { HeaderBackColor = Color.MintCream, HeaderText = "PageHeader" });
            int header1Id = doc.AddBand(new ReportBand(ReportBandType.PageHeader) { OwnerBandId = headerId, LogicalHeight = 20f, HeaderText = "Even", HeaderBackColor = Color.MistyRose });
            int header2Id = doc.AddBand(new ReportBand(ReportBandType.PageHeader) { OwnerBandId = headerId, LogicalHeight = 20f, HeaderText = "Odd", HeaderBackColor = Color.Peru });
            int bodyId = doc.AddBand(new ReportBand(ReportBandType.Detail) { LogicalHeight = 20f, ColumnCount = 2, HeaderText = "Detail Row" });
            int footerId = doc.AddBand(new ReportBand(ReportBandType.PageFooter) { HeaderBackColor = Color.SeaShell, HeaderText = "PageFooter" });
            int footer1Id = doc.AddBand(new ReportBand(ReportBandType.PageFooter) { OwnerBandId = footerId, HeaderBackColor = Color.SeaShell, LogicalHeight = 10f, HeaderText = "Even" });
            int footer2Id = doc.AddBand(new ReportBand(ReportBandType.PageFooter) { OwnerBandId = footerId, HeaderBackColor = Color.SteelBlue, LogicalHeight = 30f, HeaderText = "Odd" });

            Font font = SystemFonts.CaptionFont;
            string fnt = Convertor.FontToString(font);
            fnt = "Arial CE;12;Point;Italic;1";
            Font font2 = (Font)Convertor.StringToFont(fnt);

            IEnumerable<IReportObject> objList = Noris.Tools.FrxEditor.ReportObjects.ReportObject.ReportObjects;
            int cnt = objList.Count();

            SizeF header1Size = doc.GetBand(header1Id).LogicalSize;
            SizeF bodySize = doc.GetBand(bodyId).LogicalSize;
            doc.AddComponent(new ReportComponent(doc, new ReportObjects.ReportObjectLabel() { Alignment = ContentAlignment.TopLeft, Text = "Hokus Pokusný text, kde měříme jeho velikost. Hokus Pokusný text, kde měříme jeho velikost.", Font = new Font("Arial", 8f, FontStyle.Regular, GraphicsUnit.Point) }) { OwnerTopBandId = header1Id, RelativeBounds = RelativeRectangleF.CreateRelative(new RectangleF(0f, 4f, 200f, 9f), header1Size) });
            doc.AddComponent(new ReportComponent(doc, new ReportObjects.ReportObjectShape() { Shape = ReportObjects.ShapeType.BevelEdge, ShapeEdge = 2.5f }) { OwnerTopBandId = bodyId, RelativeBounds = RelativeRectangleF.CreateRelative(new RectangleF(10f, 2f, 90f, 7f), bodySize), AnchorY = AnchorType.Begin });
            doc.AddComponent(new ReportComponent(doc, new ReportObjects.ReportObjectLabel() { Alignment = ContentAlignment.MiddleCenter, Text = "Ukázka centrovaného textu" }) { OwnerTopBandId = bodyId, RelativeBounds = RelativeRectangleF.CreateRelative(new RectangleF(15f, 3f, 80f, 6f), bodySize), AnchorY = AnchorType.Both });
            doc.AddComponent(new ReportComponent(doc, new ReportObjects.ReportObjectShape() { Shape = ReportObjects.ShapeType.RoundEdge, ShapeEdge = 3.0f, BackColor = Color.Aquamarine, BorderColor = Color.DarkCyan }) { OwnerTopBandId = bodyId, RelativeBounds = RelativeRectangleF.CreateRelative(new RectangleF(20f, 10f, 70f, 8f), bodySize), AnchorY = AnchorType.After });

            return doc;
        }
        /// <summary>
        /// Vytvoří a vrátí sample dokument, reálnější
        /// </summary>
        /// <returns></returns>
        public static ReportDocument CreateSampleDocument3()
        {
            ReportDocument doc = new ReportDocument();
            /*
            int headerId = doc.AddBand(new ReportBand() { HeaderBackColor = Color.MintCream, HeaderText = "PageHeader" });
            int header1Id = doc.AddBand(new ReportBand() { OwnerBandId = headerId, LogicalWidth = 210f, LogicalHeight = 10f, HeaderText = "Main", HeaderBackColor = Color.MistyRose });
            int header2Id = doc.AddBand(new ReportBand() { OwnerBandId = headerId, LogicalWidth = 210f, LogicalHeight = 19f, HeaderText = "Subhead", HeaderBackColor = Color.Peru });
            int bodyId = doc.AddBand(new ReportBand() { LogicalHeight = 8f, LogicalWidth = 210f, HeaderText = "Detail Row" });
            int footerId = doc.AddBand(new ReportBand() { HeaderBackColor = Color.SeaShell, HeaderText = "PageFooter" });
            int footer1Id = doc.AddBand(new ReportBand() { OwnerBandId = footerId, HeaderBackColor = Color.SeaShell, LogicalHeight = 10f, HeaderText = "Even" });
            int footer2Id = doc.AddBand(new ReportBand() { OwnerBandId = footerId, HeaderBackColor = Color.SteelBlue, LogicalHeight = 30f, HeaderText = "Odd" });

            doc.AddComponent(new ReportComponentLabel() { OwnerTopBandId = header1Id, Bounds = new RectangleF(30f, 2f, 160f, 7f), Text = "Kalkulace dílce STPV", Font = new Font("Microsoft Sans Serif", 14f, FontStyle.Bold) });
            doc.AddComponent(new ReportComponentLabel() { OwnerTopBandId = header2Id, Bounds = new RectangleF(10f, 2f, 35f, 6f), Text = "Dílec:", Font = new Font("Microsoft Sans Serif", 12f, FontStyle.Bold) });
            doc.AddComponent(new ReportComponentLabel() { OwnerTopBandId = header2Id, Bounds = new RectangleF(47f, 2f, 90f, 6f), Text = "650001: Příčník T250", Font = new Font("Microsoft Sans Serif", 12f, FontStyle.Bold) });
            doc.AddComponent(new ReportComponentShape() { OwnerTopBandId = header2Id, Bounds = new RectangleF(8f, 1f, 138f, 9f), Shape = ReportComponentShape.ShapeType.BevelEdge, BorderWidth = 0.35f, BorderColor = Color.DarkBlue, ShapeEdge = 1.2f });

            doc.AddComponent(new ReportComponentLabel() { OwnerTopBandId = header2Id, Bounds = new RectangleF(5f, 10f, 35f, 7f), Text = "Operace", Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Bold) });
            doc.AddComponent(new ReportComponentLabel() { OwnerTopBandId = header2Id, Bounds = new RectangleF(42f, 10f, 18f, 7f), Text = "Čas TBC", Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Bold) });
            doc.AddComponent(new ReportComponentLabel() { OwnerTopBandId = header2Id, Bounds = new RectangleF(62f, 10f, 18f, 7f), Text = "Čas TAC", Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Bold) });
            doc.AddComponent(new ReportComponentLabel() { OwnerTopBandId = header2Id, Bounds = new RectangleF(82f, 10f, 28f, 7f), Text = "Cena operace", Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Bold) });

            doc.AddComponent(new ReportComponentField() { OwnerTopBandId = bodyId, Bounds = new RectangleF(5f, 0.4f, 35f, 7f), Text = "set1.operation", Font = new Font("Microsoft Sans Serif", 9f, FontStyle.Regular), Alignment = ContentAlignment.TopLeft });
            doc.AddComponent(new ReportComponentField() { OwnerTopBandId = bodyId, Bounds = new RectangleF(42f, 0.4f, 18f, 7f), Text = "set1.time_tbc", Font = new Font("Microsoft Sans Serif", 9f, FontStyle.Regular), Alignment = ContentAlignment.TopRight });
            doc.AddComponent(new ReportComponentField() { OwnerTopBandId = bodyId, Bounds = new RectangleF(62f, 0.4f, 18f, 7f), Text = "set1.time_tac", Font = new Font("Microsoft Sans Serif", 9f, FontStyle.Regular), Alignment = ContentAlignment.TopRight });
            doc.AddComponent(new ReportComponentField() { OwnerTopBandId = bodyId, Bounds = new RectangleF(82f, 0.4f, 28f, 7f), Text = "set1.price_op", Font = new Font("Microsoft Sans Serif", 9f, FontStyle.Regular), Alignment = ContentAlignment.TopRight });

            doc.AddComponent(new ReportComponentLabel() { OwnerTopBandId = footer1Id, Bounds = new RectangleF(5f, 10f, 35f, 7f), Text = "Celkem:", Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Bold), Alignment = ContentAlignment.TopLeft });
            doc.AddComponent(new ReportComponentField() { OwnerTopBandId = footer1Id, Bounds = new RectangleF(42f, 1f, 18f, 7f), Text = "set1.time_tbc", Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Bold), Alignment = ContentAlignment.TopRight });
            doc.AddComponent(new ReportComponentField() { OwnerTopBandId = footer1Id, Bounds = new RectangleF(62f, 1f, 18f, 7f), Text = "set1.time_tac", Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Bold), Alignment = ContentAlignment.TopRight });
            doc.AddComponent(new ReportComponentField() { OwnerTopBandId = footer1Id, Bounds = new RectangleF(82f, 1f, 28f, 7f), Text = "set1.price_op", Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Bold), Alignment = ContentAlignment.TopRight });

            doc.AddComponent(new ReportComponentLabel() { OwnerTopBandId = footer2Id, Bounds = new RectangleF(35f, 2f, 29f, 6f), IsLocked = true, Text = "Strana" });
            doc.AddComponent(new ReportComponentLabel() { OwnerTopBandId = footer2Id, Bounds = new RectangleF(66f, 2f, 29f, 6f), IsLocked = true, Text = "1 z 1" });
            doc.AddComponent(new ReportComponentShape() { OwnerTopBandId = footer2Id, Bounds = new RectangleF(33f, 1.4f, 75f, 7.5f), Shape = ReportComponentShape.ShapeType.BevelEdge, BorderWidth = 0.20f, BorderColor = Color.Gray, ShapeEdge = 1.6f });
            */
            return doc;
        }
        #endregion
    }
}
